/**
 * 
 */
package com.csair.youth.web.action;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.database.Permission;
import com.csair.youth.service.impl.PermissionServiceImpl;

/**
 * @author mickey
 *
 */
public class PermissionEditAction extends _BaseAction {
	private Permission item;
	private PermissionServiceImpl service;

	public Permission getItem() {
		return item;
	}
	public void setItem(Permission item) {
		this.item = item;
	}
	public PermissionServiceImpl getService() {
		return service;
	}
	public void setService(PermissionServiceImpl service) {
		this.service = service;
	}
	
	public String execute(){
		if(item != null && !StringUtils.isEmpty(item.getId())){
			//从数据库获取...
			item = service.getPermission(item.getId());
		}
		else item = new Permission();
		return SUCCESS;
	}
	
	public String save(){
		if(item == null) {
			this.setMessage("权限对象为空.");
			return INPUT;
		}
		if(!validInput(item)) return INPUT;
		service.savePermission(item);
		this.setMessage("已成功保存权限.");
		return SUCCESS;
	}
	
	private boolean validInput(Permission item){
		if(item == null) return false;
		boolean allValid = true;
		StringBuffer msgBf = new StringBuffer();
		String name = item.getName();
		if(StringUtils.isEmpty(name) || StringUtils.length(name)> 25){
			msgBf.append("权限名不能为空且必须在25个字符内<br/>");
			allValid = false;
		}
		String desc = item.getDescription();
		if(!StringUtils.isEmpty(desc) && StringUtils.length(desc)> 120){
			msgBf.append("权限描述必须在120个字符内<br/>");
			allValid = false;
		}
		String url = item.getUrl();
		if(!StringUtils.isEmpty(url) && url.getBytes().length > 255){
			msgBf.append("权限url必须在255个字节内<br/>");
			allValid = false;
		}
		this.setMessage(msgBf.toString());
		return allValid;
	}
}
